/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.service.core;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.ActionPage;
import com.blandware.atleap.model.core.ContentPage;
import com.blandware.atleap.model.core.Page;
import com.blandware.atleap.persistence.exception.DeleteException;
import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
import com.blandware.atleap.service.exception.BeanNotFoundException;

import java.util.Collection;
import java.util.List;

/**
 * <p>Business Delegate (Proxy) Interface to handle communication between web and
 * persistence layer.
 * </p>
 * <p><a href="PageManager.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.19 $ $Date: 2008/01/14 15:54:33 $
 */
public interface PageManager extends BaseManager {

    // P A G E S

	/**
	 * Returns collection of pages regardless of their type. Only active pages
     * are returned.
	 *
	 * @param queryInfo Object with some information for query
	 * @return Collection of pages
	 */
	public PartialCollection listPages(QueryInfo queryInfo);

    // L I N K A B L E   I T E M S

	/**
	 * Returns collection of pages, which are not content nor action
	 *
	 * @param queryInfo Object with some information for query
	 * @return Collection of linkable items
	 */
	public PartialCollection listLinkableItems(QueryInfo queryInfo);

	// C O N T E N T    P A G E S

	//~ CRUD Methods ================================================================

	/**
	 * Creates new content page
	 *
	 * @param contentPage Content page to create
	 * @param layoutId    ID of layout to create page on
	 * @return ID of created page
	 * @throws BeanNotFoundException      if layout with specified ID couldn't be found
	 * @throws BeanAlreadyExistsException if linkable item with same URI already exists
	 */
	public Long createContentPage(ContentPage contentPage, Long layoutId) throws BeanAlreadyExistsException, BeanNotFoundException;

	/**
	 * Retrieves content page with specified ID
	 *
	 * @param contentPageId The ID of content page to retrieve
	 * @return Content page or null if no content page with specified ID exists in database
	 */
	public ContentPage retrieveContentPage(Long contentPageId);

	/**
	 * Updates content page
	 *
	 * @param contentPage Content page to update
	 * @param layoutId    ID of layout to associate page with
	 * @throws BeanNotFoundException      if layout with specified ID couldn't be found
	 * @throws BeanAlreadyExistsException if page (no matter content or action) with same URI already exists
	 */
	public void updateContentPage(ContentPage contentPage, Long layoutId) throws BeanAlreadyExistsException, BeanNotFoundException;


	/**
	 * Removes content page
	 *
	 * @param contentPageId ID of content page to remove
	 * @throws BeanNotFoundException if content page with specified ID couldn't be found
	 * @throws DeleteException       if content page cannot be deleted
	 */
	public void deleteContentPage(Long contentPageId) throws BeanNotFoundException, DeleteException;

	// ~ Additional methods ================================================================

	/**
	 * Retrieves filtered/sorted collection of content pages
	 *
	 * @param queryInfo Object that contains information about how to filter and sort data
	 * @return Collection of content pages
	 */
	public PartialCollection listContentPages(QueryInfo queryInfo);

	/**
	 * Performs activation check, i.e. sets content pages active or inactive
     * according to their publication and expiration date and current activity.
	 */
	public void performContentPagesActivityCheck();

    // ~ Finders ================================================================

	/**
	 * Finds content page by URI
	 *
	 * @param contentPageUri URI of content page to search by
	 * @return Content page or null if nothing was found
	 */
	public ContentPage findContentPageByUri(String contentPageUri);


	// A C T I O N    P A G E S

	//~ CRUD Methods ================================================================

	/**
	 * Creates new action page
	 *
	 * @param actionPage Action page to create
	 * @return ID of created page
	 * @throws BeanAlreadyExistsException if linkable item with same URI already exists
	 */
	public Long createActionPage(ActionPage actionPage) throws BeanAlreadyExistsException;

	/**
	 * Retrieves action page with specified ID
	 *
	 * @param actionPageId The ID of the action page to retrieve
	 * @return Action page or null if no action page with specified ID exists in database
	 */
	public ActionPage retrieveActionPage(Long actionPageId);

	/**
	 * Updates action page
	 *
	 * @param actionPage Action page to update
	 * @throws BeanAlreadyExistsException if page (no matter content or action) with same URI already exists
	 */
	public void updateActionPage(ActionPage actionPage) throws BeanAlreadyExistsException;


	/**
	 * Removes action page
	 *
	 * @param actionPageId ID of action page to remove
     * @throws BeanNotFoundException if action page with specified ID couldn't be found
	 * @throws DeleteException if action page cannot be deleted
	 */
	public void deleteActionPage(Long actionPageId) throws DeleteException, BeanNotFoundException;

	// ~ Additional methods ================================================================

	/**
	 * Retrieves filtered/sorted collection of action pages
	 *
	 * @param queryInfo Object that contains information about how to filter and sort data
	 * @return Collection of action pages
	 */
	public PartialCollection listActionPages(QueryInfo queryInfo);

	// ~ Finders ================================================================

	/**
	 * Finds action page by URI
	 *
	 * @param actionPageUri URI of action page to search by
	 * @return Action page or null if nothing was found
	 */
	public ActionPage findActionPageByUri(String actionPageUri);


	// P A G E S

	/**
	 * Finds linkable item by URI
	 *
	 * @param pageUri URI of page to search by
	 * @return Page or null if nothing was found
	 */
	public Page findPageByUri(String pageUri);

	/**
	 * Retrieves linkable item with specified ID
	 *
	 * @param pageId The ID of page to retrieve
	 * @return Action page or null if no page with specified ID exists in database
	 */
	public Page retrievePage(Long pageId);

	/**
	 * Updates page
	 *
	 * @param page Page to update
	 * @throws BeanAlreadyExistsException if page with same URI already exists
	 */
	public void updatePage(Page page) throws BeanAlreadyExistsException;

    /**
     * Returns list of all pages with their content fields and content values
     * correctly assigned
     *
     * @return Collection of pages
     */
    public Collection listPagesFetching();

    /**
	 * Returns list of last modified pages.
	 *
	 * @param queryInfo Object with some information for query
	 * @return List of pages
	 */
	public List getLastPages(QueryInfo queryInfo);

    /**
     * Takes a list of pages and returns list of
     * {@link com.blandware.atleap.persistence.util.PageAndAnnotation} instances
     * where each instance corresponds to element in initial list. In the list,
     * annotation is value of 'annotation' field for corresponding page in
     * language with specified locale identifier.
     *
     * @param list              list of pages
     * @param localeIdentifier  identifier of language
     * @return list of PageAndAnnotation instances
     */
    public List getPagesAnnotations(List list, String localeIdentifier);

    /**
	 * Increases usage counter to some value (can be negative)
	 *
	 * @param pageId id of page to update
     * @param value the value to add to usage counter
	 * @throws BeanNotFoundException if page with specified ID couldn't be found
	 */
	public void increaseUsageCounter(Long pageId, Integer value) throws BeanNotFoundException;
}
